package org.database.manager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;
import oracle.ord.im.OrdImage;
import oracle.ord.im.OrdImageSignature;

import org.database.Statements;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 
 * @author Arek
 * 
 */
// TODO move signature generation and update to ImageManager
public class SignatureManager {
	private int IMG_COL;
	private int SIG_COL;

	public SignatureManager() {
		IMG_COL = 2;
		SIG_COL = 3;
	}

	/**
	 * generate
	 * 
	 * @param con
	 *            coonection to database
	 * @param id
	 *            //ID of row which contains picture
	 * @throws SQLException
	 */
	public void sigGeneration(Connection con, int id) throws SQLException {
		con.setAutoCommit(false);
		try {
			PreparedStatement stm;
			stm = con.prepareStatement(Statements.SELECT_ROW);
			stm.setInt(1, id);
			OracleResultSet get_image_obj_result = (OracleResultSet) stm
					.executeQuery();
			get_image_obj_result.next();
			OrdImage img_obj = (OrdImage) get_image_obj_result.getCustomDatum(
					IMG_COL, OrdImage.getFactory());
			OrdImageSignature img_sig = (OrdImageSignature) get_image_obj_result
					.getCustomDatum(SIG_COL, OrdImageSignature.getFactory());

			img_obj.setProperties();
			img_sig.generateSignature(img_obj);
			OraclePreparedStatement update_sig = (OraclePreparedStatement) con
					.prepareStatement(Statements.UPDATE_SIGNATURE);
			update_sig.setCustomDatum(1, img_sig);
			update_sig.setInt(2, id);
			update_sig.execute();
			update_sig.close();
			get_image_obj_result.close();
			stm.close();
			System.out.println("image signature generation complete.");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param con
	 *            connection to database
	 * @param id
	 *            //id if image for searching
	 * @throws SQLException
	 */
	public int signatureCompare(Connection con, int id) throws SQLException {
		float score = 0;
		float bestMatch = 100;
		Integer bestId = 0;
		Integer tmpId;
		con.setAutoCommit(false);
		try {
			PreparedStatement stm;
			PreparedStatement stm2;
			stm = con.prepareStatement(Statements.SELECT_ROW);
			stm2 = con.prepareStatement(Statements.SELECT_ALL);
			stm.setInt(1, id);

			OracleResultSet get_image_obj_result1 = (OracleResultSet) stm
					.executeQuery();
			get_image_obj_result1.next();
			OrdImage img_obj1 = (OrdImage) get_image_obj_result1
					.getCustomDatum(IMG_COL, OrdImage.getFactory());
			OrdImageSignature img_sig1 = (OrdImageSignature) get_image_obj_result1
					.getCustomDatum(SIG_COL, OrdImageSignature.getFactory());

			OracleResultSet get_image_obj_result2 = (OracleResultSet) stm2
					.executeQuery();
			img_obj1.setProperties();
			img_sig1.generateSignature(img_obj1);
			while (get_image_obj_result2.next()) {

				OrdImage img_obj2 = (OrdImage) get_image_obj_result2
						.getCustomDatum(IMG_COL, OrdImage.getFactory());
				OrdImageSignature img_sig2 = (OrdImageSignature) get_image_obj_result2
						.getCustomDatum(SIG_COL, OrdImageSignature.getFactory());
				img_obj2.setProperties();

				img_sig2.generateSignature(img_obj2);
				String attributes = "color=0.5 shape=0.5";
				score = OrdImageSignature.evaluateScore(img_sig1, img_sig2,
						attributes);
				if (score < bestMatch) {
					tmpId = get_image_obj_result2.getInt(1);
					if (tmpId != id) {
						bestMatch = score;
						bestId = tmpId;
					}
				}
			}
			System.out.println("Best score value:" + bestMatch
					+ " index of image: " + bestId);
			get_image_obj_result2.close();
			get_image_obj_result1.close();
			stm.close();
			stm2.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bestId;
	}

}
